package pl.edu.agh.sit.logic;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

public class TvProgramDAO {

	private static DatastoreService datastore = DatastoreServiceFactory
			.getDatastoreService();

	public static void addProgram(String channel, Date start, Date end,
			String description, String title) {

		Query q = new Query("TvProgram");
		q.addFilter("start", FilterOperator.EQUAL, start);
		q.addFilter("channel", FilterOperator.EQUAL, channel);

		PreparedQuery pq = datastore.prepare(q);
		Entity entity = null;
		for (Entity e : pq.asIterable()) {
			entity = e;
		}

		if (entity == null) {
			entity = new Entity("TvProgram");

			entity.setProperty("channel", channel);
			entity.setProperty("start", start);
			entity.setProperty("end", end);
			entity.setProperty("title", title);
			entity.setProperty("description", description);

			TvProgramDAO.datastore.put(entity);
		}
	}

	public static Program getProgram(String channel, Date start) {
		Query q = new Query("TvProgram");
		q.addFilter("start", FilterOperator.EQUAL, start);
		q.addFilter("channel", FilterOperator.EQUAL, channel);

		PreparedQuery pq = datastore.prepare(q);

		Entity entity = null;
		for (Entity e : pq.asIterable()) {
			entity = e;
		}

		return new Program((Date) entity.getProperty("start"),
				(Date) entity.getProperty("end"),
				(String) entity.getProperty("title"),
				(String) entity.getProperty("description"),
				(String) entity.getProperty("channel"), entity.getKey());
	}

	// TODO real implementation - now it is mock
	public static Collection<Program> getProgramsByPreferences(int brutality,
			int comedy, int romantism, int startTime, int endTime) {
		
		Date now = new Date();
		LinkedList<Program> ret = (LinkedList<Program>) getProgramsByDate(
				new Date(1000L), new Date(now.getTime() + 10000000L));
		Collections.shuffle(ret);

		return ret;
	}

	public static Collection<Program> getProgramsByDate(Date start, Date end) {
		Query q = new Query("TvProgram");
		q.addFilter("start", FilterOperator.GREATER_THAN_OR_EQUAL, start);
		// q.addFilter("end", FilterOperator.LESS_THAN_OR_EQUAL, end); //TODO
		// TODO naprawic

		PreparedQuery pq = datastore.prepare(q);

		List<Program> ret = new LinkedList<Program>();
		for (Entity entity : pq.asIterable()) {
			Program p = new Program((Date) entity.getProperty("start"),
					(Date) entity.getProperty("end"),
					(String) entity.getProperty("title"),
					(String) entity.getProperty("description"),
					(String) entity.getProperty("channel"), entity.getKey());
			p.setStoreKey(entity.getKey());
			ret.add(p);
		}

		return ret;
	}

}
